/**   
 * Copyright © 2023 eSunny Info. Tech Ltd. All rights reserved.

 * 
 * @Package: al.red4.chapter01 
 * @author: Administrator   
 * @date: 2023年4月9日 下午10:14:39
 * @desc: 二分查找
 */
package al.red4.chapter01;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdArrayIO;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdIn;

import java.util.Arrays;



public class BinarySearchS {
	
	
	
	public static int rankAndSearch(int key, int[] arr) {
		//arr需要保持有序
		Arrays.sort(arr);
		int low  = 0;
		int high = arr.length - 1;
		
		while(low < high) {
			int mid = low +  (high - low)/2;
			if (key < arr[mid]) high = mid - 1;
			else if (key > arr[mid]) low = mid + 1;
			else return mid;				
		}
		return -1;
	}
	
	
	public static void main(String[] args) {
//		In in = new In(args[0]);
//		int N = in.readInt();
//		System.out.println(rankAndSearch(N, new int[N]));
		// set the scale of the coordinate system
		StdDraw.setXscale(-1.0, 1.0);
		StdDraw.setYscale(-1.0, 1.0);
		StdDraw.enableDoubleBuffering();

		// initial values
		double rx = 0.480, ry = 0.860;     // position
		double vx = 0.015, vy = 0.023;     // velocity
		double radius = 0.05;              // radius

		// main animation loop
		while (true)  {

			// bounce off wall according to law of elastic collision
			if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
			if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy;

			// update position
			rx = rx + vx;
			ry = ry + vy;

			// clear the background
			StdDraw.setPenColor(StdDraw.GRAY);
			StdDraw.filledSquare(0, 0, 1.0);

			// draw ball on the screen
			StdDraw.setPenColor(StdDraw.BLACK);
			StdDraw.filledCircle(rx, ry, radius);

			// display and pause for 20 ms
			StdDraw.show();
			StdDraw.pause(20);
	}

}
	}
